﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using System.ServiceModel;

using SilverCrmSoap.CrmSdk;

namespace MrCRM.Silverlight
{
	public class OrganizationServiceClientFactory
	{
		string _serverUrl = ServerUtility.GetServerUrl();

		public OrganizationServiceClientFactory(string serverUrl)
		{
			this._serverUrl = serverUrl;
		}

		public OrganizationServiceClient Create()
		{
			// initialise OrganizationServiceClient
			Uri serverUri = new Uri(String.Format("{0}/XRMServices/2011/Organization.svc/web", this._serverUrl), UriKind.Absolute);

			BasicHttpBinding binding = new BasicHttpBinding(Uri.UriSchemeHttps == serverUri.Scheme ? BasicHttpSecurityMode.Transport : BasicHttpSecurityMode.TransportCredentialOnly)
			{
				MaxReceivedMessageSize = int.MaxValue,
				MaxBufferSize = int.MaxValue,
				SendTimeout = TimeSpan.FromMinutes(2)
			};

			return new OrganizationServiceClient(binding, new EndpointAddress(serverUri));
		}
	}
}
